// 问题跟踪系统的主要JavaScript文件

// 页面加载完成后执行
$(document).ready(function() {
    // 添加问题表单的提交处理
    const issueForm = $('#issue-form');
    if (issueForm.length) {
        issueForm.submit(function(e) {
            // 表单验证示例
            const title = $('#title').val().trim();
            if (!title) {
                alert('请输入问题标题');
                e.preventDefault();
                return false;
            }
        });
    }
    
    // 评论表单提交处理
    const commentForm = $('#comment-form');
    if (commentForm.length) {
        commentForm.submit(function(e) {
            const content = $('#content').val().trim();
            if (!content) {
                alert('请输入评论内容');
                e.preventDefault();
                return false;
            }
        });
    }
    
    // 日期选择器初始化
    const dateInputs = $('input[type="date"]');
    if (dateInputs.length) {
        // 设置最小日期为今天
        const today = new Date().toISOString().split('T')[0];
        dateInputs.attr('min', today);
    }
    
    // 标签选择功能
    const tagButtons = $('.tag-btn');
    if (tagButtons.length) {
        tagButtons.click(function() {
            $(this).toggleClass('active');
        });
    }
    
    // 平滑滚动
    $('a[href^="#"]').click(function(e) {
        e.preventDefault();
        const href = this.getAttribute('href');
        // 处理href="#"的情况
        if (href === '#') {
            window.scrollTo({ top: 0, behavior: 'smooth' });
            return;
        }
        const target = $(href);
        if (target.length) {
            window.scrollTo({
                top: target.offset().top - 80,
                behavior: 'smooth'
            });
        }
    });
});

// 实用工具函数
function formatDate(dateString) {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

// 切换暗黑模式
function toggleDarkMode() {
    document.body.classList.toggle('dark-mode');
}

// 过滤功能
function filterIssues() {
    const searchTerm = $('#search-input').val().toLowerCase();
    const statusFilter = $('#status-filter').val();
    const priorityFilter = $('#priority-filter').val();
    
    $('.issue-item').each(function() {
        const title = $(this).find('.issue-title').text().toLowerCase();
        const description = $(this).find('.issue-description').text().toLowerCase();
        const status = $(this).data('status');
        const priority = $(this).data('priority');
        
        let matchesSearch = title.includes(searchTerm) || description.includes(searchTerm);
        let matchesStatus = statusFilter === 'all' || status === statusFilter;
        let matchesPriority = priorityFilter === 'all' || priority === priorityFilter;
        
        $(this).toggle(matchesSearch && matchesStatus && matchesPriority);
    });
}